package com.guguskins.back.controller;


import com.guguskins.back.entity.GuAdminUser;
import com.guguskins.back.entity.vo.PageVo;
import com.guguskins.back.entity.vo.ResultVo;
import com.guguskins.back.service.GuAdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-07-16
 */
@RestController
@RequestMapping("/guAdminUser")
public class GuAdminUserController {

    @Autowired
    GuAdminUserService adminUserService;


    @GetMapping("/back/backPage")
    public ResultVo getPage(@RequestParam int p,
                            @RequestParam int size,
                            @RequestParam(value = "username", required = false) String username) {

        PageVo<GuAdminUser> page = adminUserService.getPage(p, size, username);
        return ResultVo.success(page);
    }

    @PostMapping("/back/add")
    public ResultVo add(@RequestBody GuAdminUser adminUser) {
//        System.out.println(adminUser.toString());
        int result = adminUserService.add(adminUser);
        if (result > 0) {
            return ResultVo.success("添加成功");
        }
        return ResultVo.fail("fail", "添加失败");
    }

    @PutMapping("/back/edit")
    public ResultVo edit(@RequestBody GuAdminUser adminUser) {
        int result = adminUserService.edit(adminUser);
        if (result > 0) {
            return ResultVo.success("修改成功");
        }
        return ResultVo.fail("fail", "修改失败");
    }

    @GetMapping("/back/changeRole")
    public ResultVo changeRole( @RequestParam(value = "adminId", required = false) Integer adminId, @RequestParam(value = "roleId",required = false) Integer roleId) {
        System.out.println("adminId="+adminId);
        System.out.println("roleId"+roleId);
        boolean result = adminUserService.changeRole(adminId, roleId);
        if (result) {
            return ResultVo.success("操作成功");
        }
        return ResultVo.fail("fail", "操作失败");
    }

    @PostMapping("/back/enable")
    public ResultVo enable(@RequestParam Integer[] ids) {
        int result = adminUserService.updateStatus(ids, 1);
        if (result > 0) {
            return ResultVo.success("启用成功");

        }
        return ResultVo.fail("fail", "启用失败");
    }

    @PutMapping("/back/disable")
    public ResultVo disable(@RequestParam Integer[] ids) {
        int result = adminUserService.updateStatus(ids, 0);
        if (result > 0) {
            return ResultVo.success("禁用成功");
        }
        return ResultVo.fail("fail", "禁用失败");
    }

//    @GetMapping("/back/detail/{id:\\d+}")
//    public ResultEntity getDetail(@PathVariable("id") Integer id) {
//        GuAdminUser adminUser = adminUserService.getDetail(id);
//        return new ResultEntity("success", adminUser);
//    }

    @PostMapping("/back/changePassword")
    public ResultVo changePassword(@RequestBody GuAdminUser adminUser) {
        int result = adminUserService.changePassword(adminUser);
        if (result > 0) {
            return ResultVo.success("success", "修改成功！");
        }
        return ResultVo.fail("fail", "修改失败！");
    }
}
